package com.hjy.cabinet.db;

import android.content.Context;
import android.util.Log;

import com.hjy.cabinet.db.domain.DbAlarmLog;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class AlarmLogInstance {

    private static AlarmLogInstance instance;

    private AlarmLogDbHelper dbHelper;
    private static final AtomicInteger counter = new AtomicInteger(0);
    private static long lastTimestamp = -1;

    private AlarmLogInstance() {
    }

    public static AlarmLogInstance getInstance() {
        if (instance == null) {
            synchronized (AlarmLogInstance.class) {
                if (instance == null) {
                    instance = new AlarmLogInstance();
                }
            }
        }
        return instance;
    }

    public long addAlarmLog(DbAlarmLog dbAlarmLog) {
        try {
            return dbHelper.addAlarmLog(dbAlarmLog);
        } catch (Exception e) {
            Log.e("AlarmLogInstance", "Error adding alarm log: " + e.getMessage(), e);
            return -1; // 返回-1表示失败
        }
    }

    public int updateAlarmLog(DbAlarmLog dbAlarmLog) {
        try {
            return dbHelper.updateAlarmLog(dbAlarmLog);
        } catch (Exception e) {
            Log.e("AlarmLogInstance", "Error updating alarm log: " + e.getMessage(), e);
            return 0; // 返回0表示没有行被更新
        }
    }

    public int deleteAlarmLog(int id) {
        try {
            return dbHelper.deleteAlarmLog(id);
        } catch (Exception e) {
            Log.e("AlarmLogInstance", "Error deleting alarm log: " + e.getMessage(), e);
            return 0; // 返回0表示没有行被删除
        }
    }

    public DbAlarmLog getAlarmLogById(int id) {
        try {
            return dbHelper.getAlarmLogById(id);
        } catch (Exception e) {
            Log.e("AlarmLogInstance", "Error retrieving alarm log by ID: " + e.getMessage(), e);
            return null; // 返回null表示未找到
        }
    }

    public DbAlarmLog getAlarmLogByNumber(String number) {
        try {
            return dbHelper.getAlarmLogByNumber(number);
        } catch (Exception e) {
            Log.e("AlarmLogInstance", "Error retrieving alarm log by number: " + e.getMessage(), e);
            return null; // 返回null表示未找到
        }
    }

    public List<DbAlarmLog> getAllAlarmLogs() {
        try {
            return dbHelper.getAllAlarmLogs();
        } catch (Exception e) {
            Log.e("AlarmLogInstance", "Error retrieving all alarm logs: " + e.getMessage(), e);
            return new ArrayList<>(); // 返回空的列表
        }
    }

    public List<DbAlarmLog> getAllUnsyncedAlarmLogs() {
        try {
            return dbHelper.getAllUnsyncedAlarmLogs();
        } catch (Exception e) {
            Log.e("AlarmLogInstance", "Error retrieving unsynced alarm logs: " + e.getMessage(), e);
            return new ArrayList<>(); // 返回空的列表
        }
    }

    public void setInstance(AlarmLogDbHelper alarmLogDbHelper) {
        this.dbHelper = alarmLogDbHelper;
    }

    public static String generateUniqueNumber() {
        long currentTimestamp = System.currentTimeMillis();

        try {
            if (currentTimestamp == lastTimestamp) {
                counter.incrementAndGet();
            } else {
                counter.set(0);
            }
            lastTimestamp = currentTimestamp;

            return currentTimestamp + String.format("%03d", counter.get());
        } catch (Exception e) {
            Log.e("AlarmLogInstance", "Error generating unique number: " + e.getMessage(), e);
            return String.valueOf(currentTimestamp) + "000"; // 返回当前时间戳 + "000"作为默认值
        }
    }
}